<?= $this->Form->create('fFilter') ?>
<table class="entity-table">
<thead>
    <? if (! empty($filter)): ?>
    <tr class="filter">
        <? foreach($columns as $label=>$pattern): ?>
        <th>
            <? if (isset($filter[$label])) {
                $f   = @$filter[$label];
                $key = $pattern;
                if (is_array($key)) {
                    $key = $key[0];
                }
                $class = @$filterClass[$label];
                
                echo $this->Form->text('Filter.'.$key, array(
                    'value'       => $f,
                    'required'    => false,
                    'style'       => 'width:100%',
                    'class'       => $class
                ));
            } ?>
        </th>
        <? endforeach; ?>
        <th>
            <?= $this->Form->button('Filtriraj') ?>
            <?= $this->Html->link('poništi', array('controller' => $entity, 'action' => 'index')) ?>
        </th>
    </tr>
    <? endif ?>
    <tr>
        <? foreach($columns as $label=>$pattern): ?>
        <?php
            // column width
            $width = '';
            if (! empty($columnWidth[$label])) {
                $width = ' width="'.$columnWidth[$label].'"';
            }
            
            $sortKey = $pattern;
            if (is_array($pattern)) {
                $sortKey = $pattern[0];
            }
            if (is_callable($pattern)) {
                $sortKey = false;
            }
            $textAlign = empty($align[$label]) ? '' : $align[$label];
        ?><th<?= $width ?> style="text-align:<?= $textAlign ?>"><?= empty($sortKey) ? $label : $this->Paginator->sort($sortKey, $label) ?></th>
        <? endforeach; ?>
        <th></th>
    </tr>
</thead>
<tbody>
    
    <? if (empty($data)): ?>
        <tr><td colspan="<?= count($columns)+1 ?>">Prazno.</td></tr>
    <? else: ?>
        <? foreach($data as $row): ?>
        <?php
            $entityId = Set::extract($row, $id);
        ?><tr>
            <? foreach($columns as $label=>$pattern): ?>
            <?
                if (is_callable($pattern)) {
                    $value = $pattern($row);
                } elseif (is_array($pattern)) {
                    $value = Set::extract($pattern[0], $row);
                    if (is_callable($pattern[1])) {
                        $value = $pattern[1]($row);
                    } else {
                        $value = sprintf($pattern[1], $value);
                    }
                } else {
                    $value = Set::extract($pattern, $row);
                }
                $link = false;
                if (! empty($links[$label])) {
                    $link    = $links[$label];
                    $params  = array($link[0]);
                    foreach (array_slice($link, 1) as $pattern) {
                        $params[] = Set::extract($pattern, $row);
                    }
                    $link    = call_user_func_array('sprintf', $params);
                }
                $textAlign = empty($align[$label]) ? '' : $align[$label];
            ?>
            <td align="<?= $textAlign ?>"><?= $link ? $this->Html->link($value, $link) : $value ?></td>
            <? endforeach; ?>
            <td class="actions button-set">
                <? if (! empty($actions)) {
                    foreach ($actions as $a) {
                        echo $this->App->buttonLink($a['label'], sprintf($a['link'], $entityId), empty($a['options'])?array():$a['options'], @$a['confirm']);
                    }
                } ?><?= $this->App->buttonLink('Izmeni', array(
                    'controller' => $entity,
                    'action'     => 'edit',
                    $entityId
                )) ?><? if (! @$cantDelete): ?><?= $this->App->buttonLinkConfirm('obriši', array(
                    'controller' => $entity,
                    'action'     => 'remove',
                    $entityId
                ), array(
                    'class' => 'delete'
                ), 'Potvrdite brisanje?') ?><? endif ?>
            </td>
        </tr>
        <? endforeach; ?>
    <? endif ?>
</tbody>
</table>
<?= $this->Form->end() ?>


<? $pagination = $this->Paginator->params() ?>
<? if ($pagination['pageCount'] > 1): ?>
<div class="pagination">
    <?= $this->Paginator->numbers(array(
        'modulus' => 4,
        'first'   => '«početak',
        'last'    => 'kraj»',
        'separator' => ' '
    )) ?>
</div>
<? endif; ?>